<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div></div>
    <script>
        // 获取元素
        const div = document.querySelector('div');

        // 盒子中坐标
        let x = 0;
        let y = 0;
        div.addEventListener('touchstart', function(e) {
            // 确定盒子中触摸点坐标
            x = e.targetTouches[0].pageX - div.offsetLeft;
            y = e.targetTouches[0].pageY - div.offsetTop;
        });
        div.addEventListener('touchmove', function(e) {
            // 阻止默认滑动
            e.preventDefault();
            // 可移动距离
            let left = e.targetTouches[0].pageX - x;
            let top = e.targetTouches[0].pageY - y;
            // 边界控制
            if (left < 0) left = 0;
            if (left > window.innerWidth - div.offsetWidth) left = window.innerWidth - div.offsetWidth;
            if (top < 0) top = 0;
            if (top > window.innerHeight - div.offsetHeight) top = window.innerHeight - div.offsetHeight;
            // 盒子位置
            div.style.left = `${left}px`;
            div.style.top = `${top}px`;
        });
    </script>
</body>

</html>